package com.xinshujia.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * *************************************
 * *   @author: 野狼上校               **
 * *   @since: 2024-08-05 14:22       **
 * *   @version: 1.0                  **
 * *************************************
 * <p>
 * Cors配置（跨域配置）
 */
@Configuration
public class XsjCorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        /*配置允许跨域的规则*/
        //允许请求的头部信息
        configuration.addAllowedHeader("*");
        //允许请求的来源
        configuration.addAllowedOriginPattern("*");
        /*下面这行写法会与允许携带cookie冲突*/
        //corsConfiguration.addAllowedOrigin("*")
        //允许请求的方法
        configuration.addAllowedMethod("*");
        //是否可以携带cookie
        configuration.setAllowCredentials(true);

        //允许所有路径 + 允许跨域的规则。注册到跨域配置中
        configurationSource.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(configurationSource);
    }
}
